package com.learn.springboot.common;

import lombok.Getter;
import lombok.Setter;

/**
 * 返回结果
 *
 * @author Don
 * @date 2020/07/08
 */
@Getter
@Setter
public class R<T> {
    private Integer code;
    private String msg;
    private T data;

    public R() {
        this(RestCode.OK.code, RestCode.OK.msg, null);
    }

    public R(T data) {
        this(RestCode.OK.code, RestCode.OK.msg, data);
    }

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> ok() {
        return new R<>();
    }

    public static <T> R<T> ok(T data) {
        return new R<>(data);
    }

    public static <T> R<T> error(String msg) {
        return new R<>(RestCode.VALID_ERROR.code, msg, null);
    }

    public static <T> R<T> error(RestCode restCode) {
        return new R<>(RestCode.OK.code, RestCode.OK.msg, null);
    }
}
